var app = getApp();
var ajax = require('../../utils/ajax.js');
var util = require('../../utils/util.js');
var common = require('../../utils/common.js');
var MPage = require('../MPage.js');

Page(common.extendPage(MPage, {
    data: {
        dataLoaded: true,
        isCatHide: true,

        curTy: 0,
        curTyName: '',
        catList: [],
        articleList: [],

        page: 1,
        loading: false,
        moreLoading: false,
        noMore: false,
        group: 1,
        isLogin: ''
    },


    onShareAppMessage: function() {
        return {
            title: this.data.shop.shareTitle,
            path: "pages/index/index"
        }
    },


    onLoad: function(options) {
        this.setData({
            curTy: options.ty,
            group: options.group
        });
    },
    onShow: function() {
        this.setData({
            page: 1,
            isLogin: !!app.getUserSid()
        })
        this._getList();
    },

    onLoginSuc() {
        this.setData({
            isLogin: true
        });
    },

    _showLogin() {
        this.setData({
            isLoginShow: true
        });
    },

    _switchCat(evt) {
        var catId = evt.target.dataset.catId;

        this.setData({
            curTy: catId,
            page: 1,
            isCatHide: true,
        });

        this._getList();
    },

    _getList(options) {
        var _self = this;
        var isMore = options && options.isMore;
        var page = _self.data.page || 1;

        if (isMore) {
            _self.setData({
                moreLoading: true
            });
        } else {
            _self.setData({
                loading: true
            });
        }

        ajax.G('/openapi/CHome/articleCategoryPage', {
            catId: _self.data.curTy,
            page: _self.data.page,
            type: _self.data.group
        }, function(data) {
            var articleList = data.result.articleList || [];
            var catList = data.result.catList || [];

            var toData = {
                articleList: isMore ? _self.data.articleList.concat(articleList) : articleList,
                catList,
                noMore: articleList.length == 0,
                page: _self.data.page + 1,
                curTyName: data.result.title,
            };
            toData[isMore ? 'moreLoading' : 'loading'] = false;
            _self.setData(toData);
        });
    },
    /**
     * 跳转文章
     */
    _goArticle: function(evt) {

        var {
            artId,
            needLogin
        } = evt.currentTarget.dataset;

        var {
            isLogin
        } = this.data;
        var formId = evt.detail.formId;

        if (needLogin == 1 && !isLogin) {

            util.alert('文章需要登录才可以查看', () => {
                this._showLogin();
            });

        } else {
            wx.navigateTo({
                url: "/pages/article/article?artId=" + artId
            })
        }
        common.submitForm(formId);

    },
    /**
     * 跳转到视频
     */
    _goVideo: function(evt) {

        var {
            videoId,
            needLogin
        } = evt.currentTarget.dataset;

        var {
            isLogin
        } = this.data;
        var formId = evt.detail.formId;

        if (needLogin == 1 && !isLogin) {

            util.alert('视频需要登录才可以查看', () => {
                this._showLogin();
            });

        } else {
            wx.navigateTo({
                url: "/pages/video/video?videoId=" + videoId
            })
        }
        common.submitForm(formId);
    },
    /**
     * 加载更多
     */
    _loadMore: function() {
        if (this.data.moreLoading || this.data.loading || this.data.noMore) {
            return;
        }
        this._getList({
            isMore: true
        });
    },

    _showCat: function(evt) {
        var formId = evt.detail.formId;
        this.setData({
            isCatHide: !this.data.isCatHide
        });
        common.submitForm(formId);
    },

    _bodyTap(evt) {
        if (evt.target.dataset.tagName == 'curCat') {
            return;
        }
        this.setData({
            isCatHide: true
        });
    },
    _jump: function(evt) {
        common.jump(evt);
    }
}))